<%@page import="entities.OrderEntity"%>
<%@page import="service.OrderService"%>
<%@page import="service.SellerService"%>
<%@page import="entities.SellerEntity"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
	+ request.getServerName() + ":" + request.getServerPort()
	+ path + "/";
	String sellerName = (String) session.getAttribute("sellerName");
	String password = (String) session.getAttribute("sellerpassword");
	SellerEntity seller = new SellerEntity();
	if (sellerName == null || password == null) {
		response.sendRedirect("index.jsp");
	} else {
		seller = SellerService.confirm(sellerName, password.trim());
	}


	String[] state  = {"接单","送餐"};
%>

<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">

<title></title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<script type="text/javascript" src="<%=basePath%>_seller/js/grid.js"></script>
<script type="text/javascript" src="<%=basePath%>js/jquery.min.js"></script>
<style type="text/css">
table#grid {
	width: 1000px;
	margin: auto;
	empty-cells: show;
	border-collapse: collapse;
	border-spacing: 0;
	empty-cells: show;
	margin: auto;
}

#grid td,#grid th {
	padding: 5px;
	border: #E3E3E3 1px solid;
	font-size: 13px;
	height: 16px;
	line-height: 16px;
}

#grid tr th {
	color: #ff0000;
	background-color: #ffffcc;
	font-size: 16px;
	font-weight: bold;
	padding: 8px 0px;
}

td.user {
	color: blue;
	font-weight: bold;
	font: 14px;
}

.orderDetail {
	color: #ff0000;
	font-weight: bold;
	font: 14px;
}
</style>

</head>
<body>
	<jsp:include page="header.jsp"></jsp:include>

	<div style="display: none;" id="sellerId"><%=seller.getId() %></div>
	<div align="center"
		style="height: 30px;margin-top: 10px;font-size: 22px;">实时订单查看</div>

	<table id="grid" cellspacing="0" cellpadding="0" class="st">
		<thead>
			<tr class="st1" align="center">
				<th width="8%">下单时间</th>
				<th width="18%">订单号</th>
				<th width="20%">订餐信息</th>
				<th width="14%">收餐人信息</th>
				<th width="20%">送货地址</th>
				<th width="10%">操作</th>
				<th width="10%">备注</th>
			</tr>
		</thead>
		<tbody>
			<%	
			List<OrderEntity> orderList = OrderService.findTodayOrder(seller.getId());
			OrderEntity orderLast = null;
			if(orderList.size()>0){
				orderLast=  orderList.get(orderList.size()-1);
			}
			int i = 0;
			for (OrderEntity order : orderList) {
			%>
			<tr>
				<td><%=order.getTime()%></td>
				<td><%=order.getNo()%></td>
				<td><span class="orderDetail"><%=order.getDetail()%></span></br> 小计：<span
					class="orderDetail"><%=OrderService.orderPrice(order.getNo(),
							seller.getId())%></span>元(<%
					int orstate = order.getState();
					if (orstate == 1) {
				%>未付款<%
					} else if (orstate == 2) {
				%>已付款<%
					} else if (orstate == 3) {
				%>正准备<%
					} else if (orstate == 4) {
				%>已送餐<%
					}
				%>)</td>
				<td class="user"><%=order.getToUser()%></td>
				<td><%=order.getToAddr()%></td>
				<td>
				<%if(orstate<4){ %><input id="<%=order.getNo()%>" state="<%=orstate%>"
					class="changeState" type="button"
					value="<%=state[orstate>2?(orstate)%2:0]%>" />
					<%}else{ %>
					<input id="<%=order.getNo()%>" state="<%=orstate%>" disabled="disabled"
					 type="button" value="已送餐" />
					<%} %>
				</td>
					
				<td><%=order.getToTime()%></td>
			</tr>

			<%
				}
			%>
			<div style="display: none;" id= "timebefore"><%=orderLast.getTime() %></div>
		</tbody>
	</table>
	<script type="text/javascript">
		//grid("名称","奇数行背景","偶数行背景","鼠标经过背景","点击后背景");
		grid("grid", "#fff", "#F5F5F5", "#FFFFCC", "#f5f5f5");
		
		function test(stateStr ,orderNo){
				var sellerId = $("#sellerId").html();
				var stateNow = parseInt(stateStr);
				var state = stateNow<3?3:stateNow+1;
				//alert(sellerId+"----"+orderNo);
				
				$.ajax({
					type : "POST",
					url : "servlet/OrderServlet",
					data : "action=updateState&userName=&sellerId=" + sellerId + "&orderNo="
							+ orderNo+"&state="+state,
					error : function(error) {
						var tip = $('a#LoginTip');
						tip.attr("class", "Error");
						tip.html("操作失败，请重试");
					},
					success : function(result) {
						if (result == "success") {
							//window.history.go(-1);
							window.location.reload(true);
							return;
						}
					}
				});
		}
		
		$(document).ready(function() {
			setInterval(function() {
				var timeBefore = $("#timebefore").html();
				var sellerId  = $("#sellerId").html();
				$.ajax({
					type : "POST",
					url : "servlet/OrderServlet",
					data : "action=getNewOrder&userName=&sellerId=" + sellerId + "&timeBefore="+timeBefore,
					dataType: "json",
					error : function(error) {
					},
					success : function(result) {
						$.each(result, function(key, value){
						    if(value.id==0){
						    	return;
						    }
						    var state  = "未提交";
						    switch(value.state){
						    	case 0:break;
						    	case 1:state ="未付款"; break;
						    	case 2:state = "已付款"; break;
						    	case 3:state = "已接单，正准备";break;
						    	case 4:state = "已送餐";break;
						    	default :break;
						    }
						    
						   var buttonHtml ="";
						   if(value.state<4){ 
						   		var stateStr = "接单";
						   		if(value.state == 3 ){stateStr = "送餐";}
						  		buttonHtml = "<input id='"+value.no+"' state='"+value.state+"' class='changeState' onclick='javascript:test("+value.state+",\""+value.no+"\");' value ='"+stateStr+"' type='button'/>";
						   }else{
						   		buttonHtml = "<input id='"+value.no+"'  disabled='disabled' state='"+value.state+"' class='changeState' value ='已送餐' type='button'/>";
						   }
				           var html ="<tr><td>"+value.time+"</td><td>"+value.no+"</td>"+
									  "<td><span class='orderDetail'>"+value.detail+"</span>("+state+")</td>"+
									  "<td><span class='orderDetail'>"+value.toUser+"</span></td>"+
									  "<td><span class='orderDetail'>"+value.toAddr+"</span></td>"+
									  "<td>"+buttonHtml+"</td>"+
									  "<td><span class='orderDetail'>"+value.toTime+"</span></td>";
							$("tbody").append(html);
							
							$("#timebefore").html(value.time);
						});
					}
				});
			}, 10000);

			$(".changeState").click(function() {
				var sellerId = $("#sellerId").html();
				var orderNo = $(this).attr("id");
				var stateNow = parseInt($(this).attr("state"));
				var state = stateNow<3?3:stateNow+1;
				//alert(sellerId+"----"+orderNo);
				
				$.ajax({
					type : "POST",
					url : "servlet/OrderServlet",
					data : "action=updateState&userName=&sellerId=" + sellerId + "&orderNo="
							+ orderNo+"&state="+state,
					error : function(error) {
						var tip = $('a#LoginTip');
						tip.attr("class", "Error");
						tip.html("操作失败，请重试");
					},
					success : function(result) {
						if (result == "success") {
							//window.history.go(-1);
							window.location.reload(true);
							return;
						}
					}
				});
			});
		});
	</script>

</body>
</html>
